package com.yzdmw.toutiao.detailsPage;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.github.chrisbanes.photoview.PhotoView;
import com.yzdmw.toutiao.R;

public class ShowWebImageActivity extends Activity {
    private TextView imageTextView = null;
    private String imagePath = null;
    private PhotoView imageView = null;
    private MyTask mTask;
    private Drawable drawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_webimage);
        this.imagePath = getIntent().getStringExtra("image");

        this.imageTextView = (TextView) findViewById(R.id.show_webimage_imagepath_textview);
        imageTextView.setText(this.imagePath);
        imageView = findViewById(R.id.show_webimage_imageview);

//        ImageWatcher vImageWatcher = (ImageWatcher) findViewById(R.id.show_webimage_imageview);

        mTask = new MyTask();
        mTask.execute();
    }

    public static Drawable loadImageFromUrl(String url) throws IOException {

        URL m = new URL(url);
        InputStream i = (InputStream) m.getContent();
        Drawable d = Drawable.createFromStream(i, "src");
        return d;
    }

    private class MyTask extends AsyncTask<String, Integer, String> {

        // 方法1：onPreExecute（）
        // 作用：执行 线程任务前的操作
        @Override
        protected void onPreExecute() {

        }

        // 方法2：doInBackground（）
        // 作用：接收输入参数、执行任务中的耗时操作、返回 线程任务执行的结果
        // 此处通过计算从而模拟“加载进度”的情况
        @Override
        protected String doInBackground(String... params) {
            try {
                drawable = ShowWebImageActivity.loadImageFromUrl(imagePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        // 方法3：onProgressUpdate（）
        // 作用：在主线程 显示线程任务执行的进度
        @Override
        protected void onProgressUpdate(Integer... progresses) {

        }

        // 方法4：onPostExecute（）
        // 作用：接收线程任务执行结果、将执行结果显示到UI组件
        @RequiresApi(api = Build.VERSION_CODES.P)
        @Override
        protected void onPostExecute(String result) {
//            imageView.setImageBitmap(((BitmapDrawable) drawable).getBitmap());
            imageView.setImageDrawable(drawable);
            imageView.setEnabled(true);
            if(drawable instanceof AnimatedImageDrawable) {
                ((AnimatedImageDrawable) drawable).start();
            }
        }

        // 方法5：onCancelled()
        // 作用：将异步任务设置为：取消状态
        @RequiresApi(api = Build.VERSION_CODES.P)
        @Override
        protected void onCancelled() {
            if(drawable instanceof AnimatedImageDrawable) {
                ((AnimatedImageDrawable) drawable).stop();
            }
        }
    }
}
